package com.wxp.demo09.apply;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.wxp.demo09.R;
import com.wxp.demo09.apply.receiver.StandardReceiver;

/**
 * 1.1 收发标准广播-无序消息
 */
public class BroadStandardActivity extends AppCompatActivity implements View.OnClickListener {

    private StandardReceiver standardReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_broad_standard);
        this.setTitle("1.1 收发标准广播-无序消息");

        //设置点击事件
        findViewById(R.id.btn_send_standard).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_send_standard) {
            // 发送标准广播
            Intent intent = new Intent(StandardReceiver.STANDARD_ACTION);
            //无序消息
            this.sendBroadcast(intent);
            return;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        standardReceiver = new StandardReceiver();
        // 创建一个意图过滤器，只处理STANDARD_ACTION的广播
        IntentFilter filter = new IntentFilter(StandardReceiver.STANDARD_ACTION);
        // 注册接收器，注册之后才能正常接收广播
        this.registerReceiver(standardReceiver, filter, Context.RECEIVER_EXPORTED);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // 注销接收器，注销之后就不再接收广播
        this.unregisterReceiver(standardReceiver);
    }
}